package com.ocom.payconfig.dao;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.ocom.common.entity.payconfig.EntityDishesInfo;
import com.ocom.common.response.payconfig.OrderSummaryVo;
import com.ocom.common.vo.dishes.DishesInfoListVo;
import com.ocom.common.vo.payconfig.GetDishesPriceInfoListVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface IDaoDishesInfo extends BaseMapper<EntityDishesInfo> {

    @Select({"<script>",
            "SELECT " +
                    " a.dishes_id, " +
                    " a.merch_id, " +
                    " a.dishes_class_id, " +
                    " c.dishes_class_name, " +
                    " a.dishes_name, " +
                    " a.dishes_code, " +
                    " a.photo_url, " +
                    " a. STATUS, " +
                    " a.sort, " +
                    " a.remark, " +
                    " a.quota_min, " +
                    " a.quota_max " +
                    " FROM " +
                    " dishes_info a " +
                    " LEFT JOIN dishes_class c ON a.dishes_class_id = c.dishes_class_id " +
                    " WHERE " +
                    " a.com_id = #{comId} " +
                    " AND a.merch_id = #{merchId} " +
                    " <when test=\"classIds != null and classIds.size() &gt; 0 \" > " +
                    " AND a.dishes_class_id in  " +
                    " <foreach collection='classIds' item='id' open='(' separator=',' close=')'> " +
                    " #{id} " +
                    " </foreach> " +
                    "</when>" +
                    " <when test=\"dishesName != null \" > " +
                    " AND a.dishes_name like concat('%', #{dishesName}, '%') " +
                    "</when>" +
                    " <when test=\"dishesCode != null \" > " +
                    " AND a.dishes_name = #{dishesCode} " +
                    "</when>" +
                    " <when test=\"status != null and status != 0 \" > " +
                    " AND a.status = #{status} " +
                    "</when>" +
                    " ORDER BY " +
                    " a.dishes_class_id, " +
                    " a.sort " +
                    "</script>"})
    List<DishesInfoListVo> selectDishesInfoList(Long comId, Integer merchId, List<String> classIds, String dishesName, String dishesCode, Integer status);




    @Select({"<script>",
            "select    D.dishes_id as dishesId , D.dishes_name as dishesName , D.sort , C.parent_id as classParent , C.sort as classSort\n" +
                    "from    dishes_info    as D \n" +
                    "left join   dishes_class C    on   C.com_id = #{comId}    and    C.dishes_class_id = D.dishes_class_id" +
                    " <when test=\"dishesIds != null and dishesIds.size() &gt; 0 \" > " +
                    " AND D.dishes_id in in  " +
                    " <foreach collection='dishesIds' item='id' open='(' separator=',' close=')'> " +
                    " #{id} " +
                    " </foreach> " +
                    "</when>" +
                    "order by  classParent,classSort,sort" +
                    "</script>"})
    List<OrderSummaryVo.DishesList> getDishesList(Long comId,  List<Integer> dishesIds);

    @Select({"<script>","SELECT\n" +
            " dishes_id AS dishesId,\n" +
            " d.dishes_class_id AS dishesClassId,\n" +
            " d.merch_id AS merchId,\n" +
            " d.dishes_code AS dishesCode,"+
            " dishes_name AS dishesName,\n" +
            " d.quota_min AS quotaMin,\n" +
            " d.quota_max AS quotaMax,\n" +
            " c.dishes_class_name AS dishesClassName,\n" +
            " IF (m.merch_id &gt; 0, m.merch_name, '本单位' ) AS merchName\n" +
            " FROM\n" +
            " dishes_info AS d\n" +
            " LEFT JOIN dishes_class AS c ON c.dishes_class_id = d.dishes_class_id\n" +
            " LEFT JOIN xf_merchant_info AS m ON m.merch_id = d.merch_id\n" +
            " WHERE\n" +
            " d.com_id = #{comId}\n" +
            " AND d. STATUS = 1\n" +
            " ORDER BY\n" +
            " d.merch_id,\n" +
            " d.dishes_class_id,\n" +
            " d.sort","</script>"})
    List<DishesInfoListVo> getAllDishes(Long comId);


    @Select("select D.dishes_class_id,D.dishes_id , D.dishes_name ,D.dishes_code,P.price ,P.price_mode from dishes_info D , dishes_price P ${ew.customSqlSegment}" )
    List<GetDishesPriceInfoListVo> getDishesPriceInfoList( @Param(Constants.WRAPPER) Wrapper wrapper);
}
